package start;

import java.util.ResourceBundle;

import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class PoolDemo {
	private static final String PREFIX = "ABCDEF.";
	private Jedis jedis;
	private JedisPool pool;

	@Before
	public void setUp() {
		ResourceBundle bundle = ResourceBundle.getBundle("redis");
		if (bundle == null) {
			throw new IllegalArgumentException("[redis.properties] is not found!");
		}

		JedisPoolConfig config = new JedisPoolConfig();
		// config.setMaxActive(Integer.valueOf(bundle .getString("redis.pool.maxActive")));
		config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
		config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWaitMillis")));
		config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
		config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));

		pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));

		jedis = pool.getResource();
		jedis.auth("foobaredauth");
	}

	@Test
	public void pool() {

		String keys = "name";
		// 删数据
		jedis.del(keys);
		// 存数据
		jedis.set(keys, "snowolf");
		// 取数据
		String value = jedis.get(keys);
		System.out.println(value);
		// 释放对象池
		// pool.returnResource(jedis);
		jedis.close();
	}
}
